//
//  LJFSearchTableViewCell.m
//  GiftThat
//
//  Created by 刘景峰 on 16/1/9.
//  Copyright © 2016年 ChuXiang. All rights reserved.
//

#import "LJFSearchTableViewCell.h"
#import "LJFPeopleSearchCollectionViewCell.h"
@implementation LJFSearchTableViewCell
#pragma mark-tableview嵌套collectionview的地方
-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.itemArray = [NSMutableArray array];
        
        UICollectionViewFlowLayout *flow = [[UICollectionViewFlowLayout alloc]init];
        flow.minimumInteritemSpacing = CGFLOAT_MIN;
        flow.minimumLineSpacing = CGFLOAT_MIN;
        flow.itemSize = CGSizeMake(self.width / 4 + 10, self.height);
        
        self.collectionView = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:flow];
        self.collectionView.delegate = self;
        self.collectionView.dataSource = self;
        [self.collectionView registerClass:[LJFPeopleSearchCollectionViewCell class] forCellWithReuseIdentifier:@"LJFPeopleSearchCollectionViewCell"];
        [self.contentView addSubview:self.collectionView];
        
        
        [self getURL];
    }
    return  self;
}

-(void)layoutSubviews{
    [super layoutSubviews];
    if ([[[NSUserDefaults standardUserDefaults] objectForKey:@"Color"] isEqualToString:@"BlackColor"]) {
        self.collectionView.backgroundColor = [UIColor colorWithRed:35 / 255.0 green:34 / 255.0 blue:38 / 255.0 alpha:1.0];
        [self.collectionView reloadData];
    }else{
        self.collectionView.backgroundColor =[UIColor whiteColor];
        [self.collectionView reloadData];
    }
    self.collectionView.frame = self.bounds;
    
    
    
}


-(void)getURL{
    [CXNetWorkTool getWithUrl:@"http://api.liwushuo.com/v2/search/hot_words" parameter:nil httpHeader:nil responseType:ResponseTypeJSON success:^(id result) {
        NSDictionary *dic = [[NSDictionary alloc]initWithDictionary:[result objectForKey:@"data"]];
        self.itemArray = [dic objectForKey:@"hot_words"];
        [self.collectionView reloadData];
    } fail:^(NSError *error) {
        NSLog(@"%@",error);
    }];
}


#pragma mark-collectionview的方法

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    return self.itemArray.count;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    LJFPeopleSearchCollectionViewCell *cell =[collectionView dequeueReusableCellWithReuseIdentifier:@"LJFPeopleSearchCollectionViewCell" forIndexPath:indexPath];
    if (self.itemArray.count > indexPath.item) {
        cell.string = self.itemArray[indexPath.item];
    }
    return cell;
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    NSString *string = self.itemArray[indexPath.item];
    NSString *newString = @"http://api.liwushuo.com/v2/search/item?keyword=%@&limit=20&offset=0&sort=";
//    转码拼接（中文转utf8编码）
    NSString *newString1 = [string stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    
    NSString *newstring2 = [NSString stringWithFormat:newString,newString1];
    [self.delegate jumpToYourSearch:newstring2 Withstring:string];
}



- (void)awakeFromNib {

}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
